/* bmpsrp5a.p - BOM SUMMARY SUB-ROUTINE                                 */
/* Copyright 1986-2011 QAD Inc., Santa Barbara, CA, USA.                      */
/* All rights reserved worldwide.  This is an unpublished work.         */
/* $Id:: bmpsrp5a.p 2366 2010-11-10 01:47:19Z zzh                          $: */
/*  REVISION: 5.0       LAST EDIT: 01/09/90      MODIFIED BY: MLB *B494*/
/*  REVISION: 6.0       LAST EDIT: 10/26/90      MODIFIED BY: emb *D145*/
/*  REVISION: 7.3       LAST EDIT: 10/19/93      MODIFIED BY: ais *GG43*/
/*  REVISION: 8.6       LAST EDIT: 10/14/97      MODIFIED BY: gyk *K0ZF* */
/*  REVISION: 9.1       LAST EDIT: 08/11/00      BY: *N0KK* jyn          */
/* Old ECO marker removed, but no ECO header exists *F0PN*                    */
/* Revision: 1.4.1.7  BY: Samir Bavkar DATE: 04/15/02 ECO: *P000* */
/* Revision: 1.4.1.9  BY: Paul Donnelly (SB) DATE: 06/26/03 ECO: *Q00B* */
/* $Revision: 1.4.1.15 $ BY: Chandrakant Ingale DATE: 09/24/09 ECO: *Q3CS* */
/*-Revision end---------------------------------------------------------------*/

/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/
/*V8:ConvertMode=Report                                        */
{us/bbi/mfdeclre.i}
{us/wb/wbrp02.i}

define temp-table tt_SummarizedBOM no-undo
   field tt_part      like pk_part
   field tt_reference like pk_reference
   field tt_qty       like pk_qty
   index tt_SummarizedBOM as primary unique tt_part tt_reference
   index tt_part tt_part.

define input-output parameter table for tt_SummarizedBOM .
define input-output parameter p_comp     like ps_comp.
define input-output parameter p_site     like si_site.
define input-output parameter p_maxlevel as   integer no-undo.
define input-output parameter p_eff_date as   date label "As of Date".

define variable qty as decimal initial 1 no-undo.
define variable level as integer initial 1 no-undo.

empty temp-table tt_SummarizedBOM.

hide message no-pause.

for each bom_mstr
   where bom_domain  = global_domain
   and   bom_parent  = p_comp
no-lock with frame phead1:

   assign
      p_comp     = bom_parent
      p_maxlevel = min(p_maxlevel,999)
      level    = 1.
   if bom_parent = ""
   then
      next.
  run explodeBOM (input p_comp,
                      input level,
                      input qty).


end. /* REPEAT WITH FRAME phead1 */

PROCEDURE explodeBOM:

   define input parameter p_comp     like ps_comp     no-undo.
   define input parameter p_level    as   integer     no-undo.
   define input parameter qty        like ps_qty_per  no-undo.

   define buffer ps_mstr for ps_mstr .
   define buffer ptmstr for pt_mstr.
   define buffer ptpdet for ptp_det.

   define query q_ps_mstr
      for ps_mstr .

   for first ptmstr
      where ptmstr.pt_domain = global_domain
      and   ptmstr.pt_part   = p_comp
   no-lock:
   end. /* FOR FIRST ptmstr */

   if available ptmstr
      and ptmstr.pt_bom_code <> ""
   then
      p_comp = ptmstr.pt_bom_code.

   open query q_ps_mstr
      for each ps_mstr
      use-index ps_par
         where ps_domain = global_domain
         and   ps_par    = p_comp
      no-lock.

   get first q_ps_mstr no-lock.
   if not available ps_mstr
   then
      return.

   repeat while available ps_mstr with frame phead1 down :
      if  p_eff_date = ?
          or (p_eff_date <> ?
         and (ps_start = ?
          or ps_start <=  p_eff_date)
         and (ps_end = ?
          or  p_eff_date <= ps_end))
          and ps_ps_code = ""
         and ps_qty_per <> 0
      then do :
         for first tt_SummarizedBOM
            where tt_part      = ps_comp
            and   tt_reference = ps_ref
         exclusive-lock:
         end. /* FOR FIRST tt_SummarizedBOM */

         if not available tt_SummarizedBOM
         then do:
            create tt_SummarizedBOM.
            assign tt_part      = ps_comp
                   tt_reference = ps_ref.

         end. /* IF NOT AVAILABLE tt_SummarizedBOM */

         tt_qty = tt_qty +
              (ps_qty_per * qty * (100 / (100 - ps_scrp_pct))).

     end. /* IF  p_eff_date = ? */
     if  p_eff_date = ?
          or (p_eff_date <> ?
         and (ps_start = ?
          or ps_start <=  p_eff_date)
         and (ps_end = ?
          or  p_eff_date <= ps_end))
          and (ps_ps_code = "x" or ps_ps_code = "")
         and ps_qty_per <> 0 and p_level < 1000
         and (p_level < p_maxlevel or p_maxlevel = 0)
      then do:
         p_comp = ps_comp.
         for first ptpdet
            where ptpdet.ptp_domain = global_domain
            and   ptpdet.ptp_part   = ps_comp
            and   ptpdet.ptp_site   = p_site
         no-lock:
         end.
         if available ptpdet
         then do:
            if ptpdet.ptp_bom_code <> ""
            then
               p_comp = ptpdet.ptp_bom_code.
         end. /* IF AVAILABLE ptpdet */
         else
            for first ptmstr
               where ptmstr.pt_domain = global_domain
               and   ptmstr.pt_part   = ps_comp
            no-lock:
               if ptmstr.pt_bom_code <> ""
               then
                  p_comp = ptmstr.pt_bom_code.
            end. /* FOR FIRST ptmstr */

         if p_level < p_maxlevel
            or p_maxlevel = 0
         then do:

            run explodeBOM (input p_comp,
                                input p_level + 1,
                                input (ps_qty_per * qty * (100 / (100 - ps_scrp_pct)))).

            get next q_ps_mstr no-lock.
         end. /* IF p_level < p_maxlevel ... */
         else
            get next q_ps_mstr no-lock.

      end. /* ELSE IF ps_ps_code = "x"*/

      else

         get next q_ps_mstr no-lock.
   end.  /* REPEAT ... */

   close query q_ps_mstr.

END PROCEDURE. /* explodeBOM */

{us/wb/wbrp04.i}
